package com.base;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

/**
 * ClassName: JDBCPrepared
 * Description:
 *
 * @Author liu
 * @Create 2025/4/22 21:05
 * @Version 1.0
 */
public class JDBCPrepared {
    public static void main(String[] args) throws Exception {
        //1.注册驱动（可省）

        //2.获取连接对象
        Connection connection = DriverManager.getConnection("JDBC:mysql:///atguigu","root","15178102072Liu");

        //3.获取执行SQL语句对象
        PreparedStatement prepareStatement = connection.prepareStatement("SELECT emp_id,emp_name,emp_salary,emp_age FROM t_emp WHERE emp_name = ?");

        System.out.println("请输入员工姓名：");
        Scanner scan = new Scanner(System.in);
        String name =scan.nextLine() ;


        //4.为占位符赋值，并执行SQL语句，接受返回的结果
        prepareStatement.setString(1, name);//1==>表示第一个占位符
        ResultSet resultSet = prepareStatement.executeQuery();

        //处理结果，遍历resultSet
        while (resultSet.next()){  //resultSet.next==>表示游标指向表头，每遍历一下，游标向下一位，直到结束
            int empId = resultSet.getInt("emp_id");
            String empName = resultSet.getString("emp——name");
            double empSalary = resultSet.getDouble("emp_salary");
            int empAge = resultSet.getInt("emp_age");
            System.out.println(empId+"\t"+empName+"\t"+empSalary+"\t"+empAge);
        }
        //释放资源
        resultSet.close();
        prepareStatement.close();
        connection.close();
    }


}
